package com.chhlz.module.base.action;

import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.chhlz.core.action.BaseAction;
import com.chhlz.core.action.Json;
import com.chhlz.core.util.CommonTools;
import com.chhlz.core.util.ExceptionUtil;
import com.chhlz.module.base.model.Bill;
import com.chhlz.module.base.model.BillDish;
import com.chhlz.module.service.BillService;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Action(value = "bill", results = { @Result(name = "bill", location = "bill.html") })
@Scope("session")
@InterceptorRef("defaultStack")
public class BillAction extends BaseAction 
implements ServletRequestAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = -8512375873351849050L;
	private static final Logger LOG = Logger.getLogger(BillAction.class);
	
	private HttpServletRequest request;
	private HttpSession session = this.getSession();
	
	@Autowired
	private BillService billService;
	private Bill bill;
	public Bill getBill() {
		return bill;
	}
	public void setBill(Bill bill) {
		this.bill = bill;
	}
	private Set<BillDish> dishes = new HashSet<BillDish>();

	public void setDishes(Set<BillDish> dishes) {
		this.dishes = dishes;
	}
	/**
	 * 得到数据列表
	 */
	public void grid(){
		int total = billService.getTotal();
		List<Bill> list = billService.findAll(page, rows, sort, order);	
		writeJson(total,list);
	}
	/**
	 * 添加操作
	 */
	public void add(){
//		String prm = this.request.getParameter("dishes");
//		List<BillDish> ls = JSON.parseArray(prm, BillDish.class);
//		LOG.info("bill action add: " + this.bill);
		System.out.println("bill action add: " + Arrays.toString(dishes.toArray()));
		Enumeration<?> en = this.request.getParameterNames();
		while(en.hasMoreElements()){
			System.out.println(en.nextElement());
		}
		
		
//		Bill bill = (Bill) session.get("bill" + this.bill.getBillId());
//		bill.setConsumption(this.bill.getConsumption());
//		bill.setDescription(this.bill.getDescription());
			Json result = new Json();
			try {
//				billService.update(bill);
				result.setSuccess(true);
				result.setMsg("添加菜式成功");					
			} catch (Exception e) {
				LOG.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("添加菜式失败");					
			}
			writeJson(result);
	}
	/**
	 * 修改操作
	 */
	public void edit() {
			Json result = new Json();
			try {
//				billService.update(bill);
				result.setSuccess(true);
				result.setMsg("修改菜式成功");
				result.setSuccess(true);
			} catch (Exception e) {
				LOG.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("添加菜式失败");
			}
			writeJson(result);
	}
	/**
	 * 删除操作
	 */
	public void remove() {
		LOG.info("bill action, remove: " + this.bill);
		System.out.println("bill action, remove: " + this.bill);
			Json result = new Json();
			try {
				billService.delete(bill.getBillId());
				result.setSuccess(true);
				result.setMsg("删除菜式成功");
			} catch (Exception e) {
				LOG.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("删除菜式失败");
			}
			writeJson(result);
	}
	
	public void create(){
		String billNumber = CommonTools.genBillNumber();
		String createBillTime = CommonTools.genTime();
		Bill bill = new Bill();
		bill.setBillNumber(billNumber);
		bill.setCreateBillTime(createBillTime);
		int id = this.billService.save(bill);
		this.session.setAttribute("bill" + id, bill);
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", String.valueOf(id));
		map.put("num", billNumber);
		map.put("cre", createBillTime);
		this.writeJson(map);
	}
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	
}
